package ControlLayer;

import ModelLayer.*;
import DBLayer.*;

import java.util.ArrayList;

public class PartOrderCtrl {
	
	public PartOrderCtrl() 
	{
        
    }
	
	public ArrayList<PartOrder> getAllPartOrder()
	{
		IfDbPartOrder dbPartOrder = new DbPartOrder();
		ArrayList<PartOrder> allPartOrder = new ArrayList<PartOrder>();
		allPartOrder = dbPartOrder.getAllPartOrder(false);
		return allPartOrder;
	}
	
	public PartOrder findPartOrderByPartOrderID(int partOrderID)
    {
		IfDbPartOrder dbPartOrder = new DbPartOrder();
        return dbPartOrder.findPartOrderByPartOrderID(partOrderID, true);
    }
	
	public int updatePartOrder(int amount, double subTotal, Product product, int orderID)
    {
		IfDbPartOrder dbPartOrder = new DbPartOrder();
        PartOrder partOrder = new PartOrder();
        partOrder.setAmount(amount);
        partOrder.setSubTotal(subTotal);
        partOrder.setProduct(product);
        partOrder.setOrderID(orderID);
        return  dbPartOrder.updatePartOrder(partOrder);
    }
	
	public void insertPartOrder(int amount, double subTotal, Product product, int orderID)
    {
    	PartOrder partOrder = new PartOrder();
    	partOrder.setAmount(amount);
    	partOrder.setSubTotal(subTotal);
    	partOrder.setProduct(product);
    	partOrder.setOrderID(orderID);
 
        try
        {
            DbConnection.startTransaction();
            DbPartOrder dbPartOrder = new DbPartOrder();
            dbPartOrder.insertPartOrder(partOrder);
            DbConnection.commitTransaction();
        }
        catch(Exception e)
        {
            DbConnection.rollbackTransaction();
        }
    }
	
	public int deletePartOrder(int partOrderID)
    {
		IfDbPartOrder dbPartOrder = new DbPartOrder();
    	return dbPartOrder.deletePartOrder(partOrderID);
    }
}
